package com.rr.middle;

/**
 * @author RR
 * @date 2024/7/6 13:04
 */

import java.sql.Array;
import java.util.*;

/**
 * https://leetcode.cn/problems/group-anagrams/
 * 注意有坑  相同的字母可能出现多次  出现次数也一样才能算一起的
 */
public class GroupAnagrams {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<Map<Character,Integer>,List<String>> map = new HashMap<>();
        for (String str : strs) {
            Map<Character, Integer> charCountMap = new HashMap<>();
            int length = str.length();
            for (int i = 0; i < length; i++) {
                char key = str.charAt(i);
                Integer count = charCountMap.getOrDefault(key, 0);
                charCountMap.put(key, ++count);
            }
            map.computeIfAbsent(charCountMap, value -> new ArrayList<>()).add(str);
        }
        return new ArrayList<>(map.values());
    }

    public static void main(String[] args) {
        GroupAnagrams groupAnagrams = new GroupAnagrams();
        System.out.println(groupAnagrams.groupAnagrams(new String[]{"dgggggggggg", "ddddddddddg"}));

    }
}
